package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Assess;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.entity.Member;
import com.igeek.collateral.mapper.AssessMapper;
import com.igeek.collateral.mapper.CollateralMapper;
import com.igeek.collateral.service.IAssessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.service.ICollateralService;
import com.igeek.collateral.vo.PageQueryVO;
import liquibase.pro.packaged.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.stream.Stream;

/**
 * <p>
 * 评估报告 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class AssessServiceImpl extends ServiceImpl<AssessMapper, Assess> implements IAssessService {

@Autowired
private AssessMapper assessMapper;


@Autowired
private CollateralMapper collateralMapper;
@Transactional
    @Override
    public Page<Assess> findPage(PageQueryVO vo) {

    Integer currentPage = vo.getCurrentPage();
    Integer pageSize = vo.getPageSize();
    String queryString = vo.getQueryString();
    Page<Assess> page=new Page<>(currentPage,pageSize);
    LambdaQueryWrapper<Assess> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.like(StringUtils.hasLength(queryString),Assess::getApplyStaff,queryString);
    Page<Assess>assessPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
    return assessPage;
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Assess> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Assess::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }


    //点击确定好,实现响应的计算功能/点击否决后，实现修改两者的状态
    @Override
    public boolean updateS(Assess assess) {
        Assess Ass = assessMapper.selectById(assess.getId());
        Collateral coll = collateralMapper.selectById(Ass.getCollateralId());
        Double weights=assess.getWeights();
        Double myBank=assess.getConfirmValue();
        Double evaluateValue=assess.getEvaluateValue();
        //计算得出最终的价格
        Double endValue=(evaluateValue*weights+myBank*(1-weights))*0.7;
        Ass.setAuditStaff(assess.getAuditStaff());
        Ass.setAgencyId(assess.getAgencyId());
        Ass.setEvaluateMethod(assess.getEvaluateMethod());
        Ass.setWeights(weights);
        Ass.setEvaluateDate(assess.getEvaluateDate());
        Ass.setConfirmValue(myBank);
        Ass.setEvaluateValue(evaluateValue);
        //将计算好的价值赋值回去,先判断一下，同时也要修改押品里的处置参考价格
        //,将两个状态全改为已完成评估
            coll.setReferencePrice(endValue);
            coll.setStatus(3);
        Ass.setLatestValue(endValue);
        Ass.setStatus(3);
        //赋值成功后将两表的数据进行更新
        assessMapper.updateById(Ass);
        collateralMapper.updateById(coll);
        return Ass!=null?true:false;
    }


    //点击否决，实现的相关功能（将两者的状态全部修改为评估为通过）
    @Override
    public boolean updateDS(Assess assess) {
        Assess ass=assessMapper.selectById(assess);
        ass.setStatus(4);
        Collateral collateral=collateralMapper.selectById(assess.getCollateralId());
        //将状态改为评估未通过
        collateral.setStatus(4);
        //更新两者的状态
        assessMapper.updateById(ass);
        collateralMapper.updateById(collateral);
        return ass!=null?true:false;
    }

}
